#include "cswitchx.h"
#include "v2/xml/xml_device_object.h"

CSwitchx::CSwitchx(CKnxControl *parent) :
    CKnxControl(parent)
{
    _device_name = "灯";
}

void CSwitchx::execDeviceAction(QString action,QString value,QString unit)
{
    execKnxAction(action,value);
}

void CSwitchx::setKnxObject(void *xOBJECT)
{
    //表达式状态
    _onValue = "1";
    _offValue = "0";

    if(!_status_true.isEmpty())
    {
        _onValue = _status_true;
    }

    if(!_status_false.isEmpty())
    {
        _offValue = _status_false;
    }

    Xml_DEVICE_OBJECT *xo = (Xml_DEVICE_OBJECT *)xOBJECT;

    QStringList arrys;
    //发送值
    _sendOnArray = xo->_send.split(",");

    //反馈值
    arrys = xo->_status.split(":");
    if(arrys.size() >= 3)
    {
        _feedbackAddress = arrys.at(0);
        _feedbackDataType =  (DATA_TYPE)arrys.at(1).toUInt();
        _feedbackValueDefine =  (DATA_DEFINE)arrys.at(2).toUInt();
    }

}

void CSwitchx::readKnxAction()
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    CExecStruct *s = new CExecStruct();
    s->address = _feedbackAddress.split(":")[0];

    eThread->execList.append(s);

    eThread->start();
}

void CSwitchx::execKnxAction(QString action,QString value)
{
    qDebug() << "KNX: CSwitchx execKnxAction " << action  + " = " + value;

    if(action!= DEVICE_ACTION_SWITCH)
    {
        qDebug() << "KNX: CSwitchx action not support !";
        return ;
    }

    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    QStringList *sendArray = &_sendOnArray;

    QString sendAddress,sendValue;
    DATA_TYPE sendDataType;
    DATA_DEFINE sendValueDefine;

    //default
    sendValue = value;

    for (int i=0; i<sendArray->size(); i++)
    {
        QString dev = sendArray->at(i);
        QStringList devArray = dev.split(":");

        if (devArray.size() >= 3) {
            sendAddress = devArray.at(0);

            if (sendAddress == "msleep") {
                CExecStruct *s = new CExecStruct();
                s->address = dev;

                eThread->execList.append(s);
            }
            else
            {
                sendDataType = (DATA_TYPE)devArray.at(1).toUInt();
                sendValueDefine = (DATA_DEFINE)devArray.at(2).toUInt();

                if(devArray.size() > 3)
                {
                    QStringList sl = devArray.at(3).split("|");
                    if(sl.size() == 2)
                    {
                        if(value == DEVICE_STATUS_ON)
                        {
                            sendValue = sl.at(0);
                        }else{
                            sendValue = sl.at(1);
                        }
                    }else{
                        sendValue = devArray.at(3);
                    }
                }

                if(!sendAddress.isEmpty())
                {
                    int data_len = 0;
                    memset(_data_buf,0,sizeof(_data_buf));

                    qDebug() << "CSwitchx knx write " << sendValue;
                    if((data_len = DEVICE_OBJECT::KnxQStringToData(sendValue,sendDataType,sendValueDefine,(_u8 *)_data_buf)) == -1)
                    {
                        qDebug() << "SetDev ret='-1' msg='error data args'" ;
                        continue ;
                    }

                    CExecStruct *s = new CExecStruct();
                    s->address = sendAddress;
                    s->type = sendDataType;
                    s->data = QByteArray((const char *)_data_buf,data_len);

                    eThread->execList.append(s);
                }
            }
        }

    }

    eThread->start();

}

QString CSwitchx::actionFromStatus(QString address,QString value)
{
    return DEVICE_ACTION_SWITCH;
}

QString CSwitchx::valueCheck(QString action,QString value)
{
    if(value == _onValue)
        return DEVICE_STATUS_ON;
    else
        return DEVICE_STATUS_OFF;
}
